package com.halixun.d2app.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.halixun.d2app.dialog.LoadingFragment;
import com.halixun.d2app.ui.fragment.CommonFragment;

/**
 * @ProjectName: D2App
 * @Package: com.halixun.d2app.base
 * @ClassName: BaseFragment
 * @Description: java类作用描述
 * @Author: hdx
 * @CreateDate: 2019/11/28 18:11
 * @UpdateUser: hdx
 * @UpdateDate: 2019/11/28 18:11
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public abstract class BaseFragment extends Fragment {
    protected abstract void initView();

    protected abstract void setListeners();

    protected boolean isInitView = false;

    protected View mRootView;

    protected Context mContext;


    private LoadingFragment mLoadingFragment;

    private final String mLoadingDialogTag = "loadingDialog";



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.mRootView = view;
        initView();
        setListeners();
        isInitView = true;
    }

    protected int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    @SuppressLint("NewApi")
    public void showProgressDialog(String message) {
        LoadingFragment f = (LoadingFragment) getChildFragmentManager().findFragmentByTag(mLoadingDialogTag);
        if (null == f) {
            mLoadingFragment = LoadingFragment.getInstance(message);
            mLoadingFragment.show(getChildFragmentManager(), mLoadingDialogTag);
        } else {
            if (null == mLoadingFragment){
                mLoadingFragment = f;
            }
            if (mLoadingFragment.isHidden()){
                mLoadingFragment.setTextMessage(message);
                getChildFragmentManager().beginTransaction().show(mLoadingFragment);
            } else {
                mLoadingFragment.setTextMessage(message);
            }
        }
    }

    @SuppressLint("NewApi")
    public void dismissProgressDialog() {
        LoadingFragment f = (LoadingFragment) getChildFragmentManager().findFragmentByTag(mLoadingDialogTag);
        if (null != f) {
            f.dismiss();
            f = null;
        }
        mLoadingFragment = null;
    }

//
//
//
//
//    @SuppressLint("NewApi")
//    public void showProgressDialog(String message) {
//        if (mProgressDlg == null && !getActivity().isFinishing() && !getActivity().isDestroyed()) {
//            mProgressDlg = new ProgressDialog(getActivity());
//            mProgressDlg.setMessage(message);
//            mProgressDlg.setCanceledOnTouchOutside(false);
//            // mProgressDlg.setCanceledOnTouchOutside(false);
//            // mProgressDlg.setCancelable(false);
//            mProgressDlg.show();
//        }
//    }
//
//
//    @SuppressLint("NewApi")
//    public void setProgressDialogMessage(String message) {
//        if (mProgressDlg != null && mProgressDlg.isShowing()) {
//            Context context = ((ContextWrapper) mProgressDlg.getContext()).getBaseContext();
//            if (context instanceof Activity) {
//                if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed())
//                    mProgressDlg.setMessage(message);
//            } else {
//                mProgressDlg.setMessage(message);
//            }
//        }
//    }
//
//    @SuppressLint("NewApi")
//    public void dismissProgressDialog() {
//        if (mProgressDlg != null && mProgressDlg.isShowing()) {
//            Context context = ((ContextWrapper) mProgressDlg.getContext()).getBaseContext();
//            if (context instanceof Activity) {
//                if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed())
//                    mProgressDlg.dismiss();
//            } else {
//                mProgressDlg.dismiss();
//            }
//        }
//        mProgressDlg = null;
//    }
}
